<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>打印功能</title>
    <style>
        #outer {
            display:table;
            height:400px;
            overflow:hidden;
        }
        #middle {
            display:table-cell;
            vertical-align:middle;
        }
    </style>
</head>
<body>
<!--startprint-->
<div id="outer">
    <div id="middle">
        <div id="inner">
            这里是要垂直居中的内容
        </div>
    </div>
</div>
<!--endprint-->
<button onclick="javascript:preview();">打印</button>
<script>
    function preview() {
        var bdhtml = window.document.body.innerHTML;//获取当前页的html代码
        var sprnstr = '<!--startprint-->';//设置打印开始区域
        var eprnstr = '<!--endprint-->';//设置打印结束区域
        var prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 18); //从开始代码向后取html
        prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));//从结束代码向前取html
        window.document.body.innerHTML = prnhtml;
        window.print();
        window.document.body.innerHTML = bdhtml;
    }
</script>
</body>
</html>